<?php

class Frontend extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->initialization();
    }

    private function initialization() {
        $lang = $this->input->cookie('lang');
        $lang = empty($lang) ? 'zh' : $lang;

        $this->set_title('筑奥设计');
        $this->set_layout('frontend/layout_view');
        $this->set_javascript_variable('SITE_URL', SITE_URL);
        $this->set_javascript_variable('USER_ID', intval($this->session->userdata('user_id')));
        $this->set_javascript_variable('LANG', $lang);
        $this->load_javascripts(array('jquery', 'jquery.tmpl', 'lib', 'frontend_layout'));
        $this->load_stylesheets(array('z-lib', 'global', 'frontend_layout'));

        $this->load->model('label_model');
        $this->lang->load('ui', $lang=='en'?'english':'chinese');

        $types = $this->label_model->records(0, 0, array(array('parent', 1)));
        $areas = $this->label_model->records(0, 0, array(array('parent', 2)));

        foreach ($types as &$type) {
            if ($lang == 'en') {
                $type->value = $type->value_en;
            }
            unset($type->vlaue_en);
        }

        foreach ($areas as &$area) {
            if ($lang == 'en') {
                $area->value = $area->value_en;
            }
            unset($area->vlaue_en);
        }

        $this->load->model('member_model');
        $members = $this->member_model->records();
        foreach ($members as &$member) {
            if ($lang == 'en') {
                $member->label = $member->label_en;
            }
            unset($member->label_en);
        }

        $this->set_data(array('types' => $types, 'areas' => $areas, 'members' => $members, 'g_lang' => $lang));

        $this->load->helper(array('text', 'template', 'z_text'));
    }

    public function language() {
        //通过get参数来设置语言
        $lang = $this->input->get('lang');
        if ($lang) {
            $this->input->set_cookie(array(
                'name' => 'lang',
                'value' => $lang,
                'expire' => '86500'
            ));
        } else {
            //没有参数 则去cookie中的值
            $lang = $this->input->cookie('lang');
            if (!$lang) {
                $lang = 'zh';
            }
        }

        header("Location: " . SITE_URL . '/index.php');
        exie(0);
    }

}
